<?php

namespace app\admin\controller\wanlshop;

use addons\wanlshop\library\WanlChat\WanlChat;
use addons\wanlshop\library\WanlPay\WanlPay;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\UserFinanceLog;
use think\Db;
use think\Exception;

use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 订单退款管理
 *
 * @icon fa fa-circle-o
 */
class Refund extends Backend
{
    /**
     * Refund模型对象
     * @var \app\admin\model\wanlshop\Refund
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wanlshop\Refund;
        $this->wanlchat = new WanlChat();
        $this->view->assign("expresstypeList", $this->model->getExpresstypeList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("reasonList", $this->model->getReasonList());
        $this->view->assign("stateList", $this->model->getStateList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['user', 'order', 'pay', 'shop'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['user', 'order', 'pay', 'shop'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
                if ($row['order_type'] === 'groups') {
                    $row->groupsgoods = $row->groupsgoods ? $row->groupsgoods->visible(['title', 'image']) : [];
                } else {
                    $row->goods = $row->goods ? $row->goods->visible(['title', 'image']) : [];
                }
                $row->getRelation('user')->visible(['id', 'username']);
                $row->getRelation('order')->visible(['id']);
                $row->getRelation('pay')->visible(['pay_no']);
                $row->getRelation('shop')->visible(['shopname']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 退款详情
     */
    public function detail($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row['images'] = explode(',', $row['images']);

        if ($row['order_type'] === 'groups') {
            $row['ordergoods'] = model('app\admin\model\wanlshop\GroupsOrderGoods')
                ->where('id', 'in', $row['goods_ids'])
                ->select();
        } else {
            $row['ordergoods'] = model('app\admin\model\wanlshop\OrderGoods')
                ->where('id', 'in', $row['goods_ids'])
                ->select();
        }

        $row['log'] = model('app\admin\model\wanlshop\RefundLog')
            ->where(['refund_id' => $ids])
            ->order('createtime desc')
            ->select();
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 同意退款
     */
    public function agree($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        //退款状态:0=申请退款,1=卖家同意,2=卖家拒绝,3=申请平台介入,4=成功退款,5=退款已关闭,6=已提交物流,7=第三方退款中,8=退款失败
        if ($row['state'] != 3) {
            $this->error(__('当前状态，不可操作' . $row['state']));
        }
        // 判断金额
        if (number_format($row['price'], 2) > number_format($row->pay->price, 2)) {
            $this->error(__('非法退款金额，金额超过订单金额！！'));
        }
        $result = false;
        Db::startTrans();
        $library = new WanlPay();
        $order = \app\admin\model\wanlshop\Order::find($row->order_id);
        try {
            // 判断退款类型0=我要退款(无需退货),1=退货退款
            if ($row['type'] == 0) {
                // 退款完成
                $state = 4;
                // 退款日志
                $refundLog = '平台判定卖家需配合买家完成退货';
                // 退款类型
                $refundType = 2;
                $orderGoodsState = 3;

                // 返还资金，并写入日志，未收货前资金等于冻结在平台无需处理卖家资金流向
                $library::money(+$row['price'], $row['user_id'], '客服判定同意退款', 'refund', $row['pay']['order_no']);

                //退积分
                $this->returnScore($row['order_id'], $row['user_id'], $ids);

                // 1.1.5升级 第三方退款
                $config = get_addon_config('wanlshop');
                // 检查是否原路返还 TOdo 都退回余额
                if ($config['config']['refund_switch'] == 'Y') {
                    $refundType = 3;
                    $pay = model('app\api\model\wanlshop\Pay')->get($row['order_pay_id']);
                    if ($pay['pay_type'] != 0) {
                        $wanlpay = $library->refund($row['id'], $row['price'], $row['order_pay_id']);
                        // code=0同步方法：refund判断fund_change = Y，代表退款成功，支付宝文档《如何判断退款是否成功》，https://opendocs.alipay.com/support/01rawa
                        if ($wanlpay['code'] == 0) {
                            // 退款中
                            $state = 4;
                            // 退款日志
                            $refundLog = '平台判定卖家需配合买家完成退货，已提交' . $wanlpay['data']['type_text'] . '已将退款￥' . $wanlpay['data']['money'] . '元原路返还买家' . $wanlpay['data']['type_text'] . '账户';
                            // 扣除本地用户余额
                            $library::money(-$wanlpay['data']['money'], $wanlpay['data']['user_id'], '退款订单（订单号：' . $order['order_no'] . '）已原路返回余额至你的' . $wanlpay['data']['type_text'], 'sys');
                            // 如果退款成功,修改退款状态为第三方退款中，等待回调,否则退款到余额
                        } else if ($wanlpay['code'] == 200) {
                            // 退款中
                            $state = 7;
                            // 退款日志
                            $refundLog = '平台判定卖家需配合买家完成退货，已提交' . $wanlpay['data']['type_text'] . '处理，预计24小时内将退款' . $wanlpay['data']['money'] . '元原路返还给买家' . $wanlpay['data']['type_text'] . '账户';
                            // 扣除本地用户余额
                            $library::money(-$wanlpay['data']['money'], $wanlpay['data']['user_id'], '退款订单（订单号：' . $order['order_no'] . '）已原路返回余额至你的' . $wanlpay['data']['type_text'], 'sys');
                        } else {
                            $refundLog = '平台判定卖家需配合买家完成退货，第三方支付退款失败已转入余额，请手动提现';
                        }
                    }
                }
                //后续版本推送订购单
                // ...
            } else if ($row['type'] == 1) {
                // 先同意退款，还需要买家继续退货
                $state = 1;
                $orderGoodsState = 2;
            } else {
                $this->error(__('非法退款类型'));
            }

            // 只有本地余额退款状态4,和先同意退款,需要商家继续退款状态1执行,状态7原路返回在回调更新状态
            // 退款状态:0=申请退款,1=卖家同意,2=卖家拒绝,3=申请平台介入,4=成功退款,5=退款已关闭,6=已提交物流,7=第三方退款中,8=退款失败
            if ($state == 1 || $state == 4) {
                // 更新商品状态
                $this->setOrderGoodsState($orderGoodsState, $row['goods_ids'], $row['order_type'], $row['order_id']);
                // 更新订单状态
                if ($row['type'] == 0) {
                    $this->setRefundState($row['order_id'], $row['order_type']);
                }

                //退回换购商品、优惠券
                (new \app\api\model\wanlshop\Order())->returnBuyback($row['order_id'], $row['user_id']);
            }

            // 写入日志
            $this->refundLog($row['user_id'], $row['id'], $refundLog ?? '', $refundType ?? 2, $row['shop_id']);
            // 更新退款
            $result = $row->allowField(true)->save([
                'state' => $state,
                'completetime' => time()
            ]);

            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result !== false) {
            $this->success();
        } else {
            $this->error(__('No rows were updated'));
        }
    }

    /**
     * 平台判定拒绝退款
     */
    public function refuse($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($row['state'] != 3) {
            $this->error(__('当前状态，不可操作'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $result = false;
                Db::startTrans();
                try {
                    // 写入日志
                    $this->refundLog($row['user_id'], $row['id'], '客服判定：' . $params['refund_content'], $row['shop_id']);
                    // 更新商品状态
                    $this->setOrderGoodsState(4, $row['goods_ids'], $row['order_type'], $row['order_id']);
                    // 更新订单状态
                    $this->setRefundState($row['order_id'], $row['order_type']);
                    // 更新退款
                    $result = $row->allowField(true)->save(['state' => 5]);

                    Db::commit();
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 更新订单商品状态（方法内使用）1.0.5升级
     *
     * @ApiSummary  (WanlShop 更新订单商品状态)
     * @ApiMethod   (POST)
     *
     * @param string $status 状态
     * @param string $goods_id 商品ID
     */
    private function setOrderGoodsState($status = 0, $goods_id = 0, $order_type = 'goods', $order_id = 0)
    {
        if ($order_type === 'groups') {
            $orderGoodsModel = model('app\admin\model\wanlshop\GroupsOrderGoods');
        } else {
            $orderGoodsModel = model('app\admin\model\wanlshop\OrderGoods');
        }
        return $orderGoodsModel->save(['refund_status' => $status], ['order_id' => $order_id]);
    }

    /**
     * 修改订单状态（方法内使用） 1.0.5升级
     *
     * @ApiSummary  (WanlShop 修改订单状态)
     * @ApiMethod   (POST)
     *
     * @param string $id 订单ID
     */
    private function setRefundState($order_id = 0, $order_type = 'goods')
    {
        if ($order_type === 'groups') {
            $orderModel = model('app\admin\model\wanlshop\GroupsOrder');
            $orderGoodsModel = model('app\admin\model\wanlshop\GroupsOrderGoods');
        } else {
            $orderModel = model('app\admin\model\wanlshop\Order');
            $orderGoodsModel = model('app\admin\model\wanlshop\OrderGoods');
        }

        //todo 整单退款
        $orderModel->save(['state' => 5], ['id' => $order_id]);
        return true;

        $list = $orderGoodsModel
            ->where(['order_id' => $order_id])
            ->select();
        $refundStatusCount = 0;
        foreach ($list as $row) {
            // 退款状态:0=未退款,1=退款中,2=待退货,3=退款完成,4=退款关闭,5=退款被拒
            if ($row['refund_status'] == 3) $refundStatusCount += 1;
        }
        // 订单状态:1=待支付,2=待发货,3=待收货,4=待评论,5=售后订单(已弃用),6=已完成,7=已取消 1.0.5升级
        // 如果订单下所有商品全部退款完毕则关闭订单
        if (count($list) == $refundStatusCount) {
            $orderModel->save(['state' => 5], ['id' => $order_id]);
            return true;
        }
        return false;
    }

    /**
     * 退款日志（方法内使用）
     *
     * @ApiSummary  (WanlShop 退款日志)
     *
     * @param string $user_id 用户ID
     * @param string $refund_id 退款ID
     * @param string $content 日志内容
     * @param string $type 退款状态:0=买家,1=卖家,2=官方,3=系统
     */
    private function refundLog($user_id = 0, $refund_id = 0, $content = '', $type = 2, $shop_id = 0)
    {
        return model('app\admin\model\wanlshop\RefundLog')->allowField(true)->save([
            'user_id' => $user_id,
            'refund_id' => $refund_id,
            'shop_id' => $shop_id,
            'type' => $type,
            'content' => $content
        ]);
    }

    /**
     * 同意退款
     */
    public function shopagree($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        //退款状态:0=申请退款,1=卖家同意,2=卖家拒绝,3=申请平台介入,4=成功退款,5=退款已关闭,6=已提交物流,7=第三方退款中,8=退款失败
        if ($row['state'] != 0) {
            $this->error(__('当前状态，不可操作' . $row['state']));
        }
        // 判断金额
        // if(number_format($row['price'], 2) > number_format($row->pay->price, 2)){
        // 	$this->error(__('非法退款金额，金额超过订单金额！！请拒绝退款！！'));
        // }
        $result = false;
        $library = new WanlPay();
        $error = '';
        Db::startTrans();

        if ($row['order_type'] === 'groups') {
            // 查询订单是已确定收货
            $order = model('app\index\model\wanlshop\groups\Order')->get($row['order_id']);
        } else {
            $order = model('app\index\model\wanlshop\Order')->get($row['order_id']);
        }

        try {
            // 判断退款类型 我要退款(无需退货)
            if ($row['type'] == 0) {
                //order_goods.refund_status退款状态:0=未退款,1=退款中,2=待退货,3=退款完成,4=退款关闭,5=退款被拒
                $refund_status = 3;

                //refund.state退款状态:0=申请退款,1=卖家同意,2=卖家拒绝,3=申请平台介入,4=成功退款,5=退款已关闭,6=已提交物流,7=第三方退款中,8=退款失败
                $data['state'] = 4; // 退款完成
                $data['completetime'] = time(); // 完成退款 时间
                // 退款日志
                $refundLog = '卖家同意退款，' . $row['price'] . '元退款到买家账号余额';
                //refund_log.type退款状态:0=买家,1=卖家,2=官方,3=系统
                $refundType = 1;
                // 推送标题
                $pushLog = '退款已完成';
                // // 查询订单是已确定收货
                if ($row['order_type'] === 'groups') {
                    // group_order.state 订单状态:1=待支付,2=待成团,3=待发货,4=待收货,5=待评论,6=已完成,7=已取消
                    $orderPay = in_array($order['state'], [5, 6]);
                } else {
                    // 订单状态:1=待支付,2=待发货,3=待收货,4=待评论,5=已退款,6=已完成,7=已取消
                    $orderPay = in_array($order['state'], [4, 6]);
                }
                // 更新钱包 1.此订单如果已确认收货扣商家 2.此订单没有确认收货，平台退款
                if ($orderPay) {
                    // 扣商家
                    $library::money(-$row['price'], $order['shop']['user_id'], '确认收货，同意退款', 'refund', $order['order_no']);
                }

                // 退款给用户 (如果第三方退款提交成功后，扣除此款项)
                $library::money(+$row['price'], $row['user_id'], '卖家同意退款', 'refund', $order['order_no']);

                //退积分
                $this->returnScore($row['order_id'], $row['user_id'], $ids);

                // 1.1.5升级 第三方退款
                $config = get_addon_config('wanlshop');
                // 检查是否原路返还
                //todo 退款暂时都退到余额
                if ($config['config']['refund_switch'] == 'Y') {
                    //refund_log.type退款状态:0=买家,1=卖家,2=官方,3=系统
                    $refundType = 3;
                    $pay = model('app\api\model\wanlshop\Pay')->get($row['order_pay_id']);
                    if ($pay['pay_type'] != 0) {
                        $wanlpay = $library->refund($row['id'], $row['price'], $row['order_pay_id']);
                        // code=0同步方法：refund判断fund_change = Y，代表退款成功，支付宝文档《如何判断退款是否成功》，https://opendocs.alipay.com/support/01rawa
                        if (!$wanlpay) {
                            $refundLog = '卖家同意退款，第三方支付退款失败' . $row['price'] . '元退款到买家账号余额，请手动提现';
                        } else {
                            if ($wanlpay['code'] == 0) {
                                // 退款中
                                $data['state'] = 4;
                                // 退款日志
                                $refundLog = $wanlpay['data']['type_text'] . '已将退款￥' . $wanlpay['data']['money'] . '元原路返还买家' . $wanlpay['data']['type_text'] . '账户';
                                // 推送日志
                                $pushLog = $wanlpay['data']['type_text'] . '退款成功';
                                // 扣除本地用户余额
                                $library::money(-$wanlpay['data']['money'], $wanlpay['data']['user_id'], '退款订单（订单号：' . $order['order_no'] . '）已原路返回余额至你的' . $wanlpay['data']['type_text'], 'sys');
                                // 如果退款成功,修改退款状态为第三方退款中，等待回调,否则退款到余额
                            } else if ($wanlpay['code'] == 200) {
                                // 退款中
                                $data['state'] = 7;
                                // 退款日志
                                $refundLog = '已提交' . $wanlpay['data']['type_text'] . '处理，预计24小时内将退款￥' . $wanlpay['data']['money'] . '元原路返还买家' . $wanlpay['data']['type_text'] . '账户';
                                // 推送日志
                                $pushLog = $wanlpay['data']['type_text'] . '正在处理您的退款预计24小时内到账';
                                // 扣除本地用户余额
                                $library::money(-$wanlpay['data']['money'], $wanlpay['data']['user_id'], '退款订单（订单号：' . $order['order_no'] . '）已原路返回余额至你的' . $wanlpay['data']['type_text'], 'sys');
                                // 支付宝提交退款,返回fund_change=N时,在此中断
                            }
                        }
                    }
                }
                //后续版本推送订购单
                // ...
            } else if ($row['type'] == 1) {
                $refund_status = 2;
                $data['state'] = 1; // 先同意退款，还需要买家继续退货
                $data['agreetime'] = time(); // 卖家同意 时间
                // 退货地址
                $shopConfig = model('app\index\model\wanlshop\ShopConfig')
                    ->where(['shop_id' => $order->shop_id])
                    ->field('returnAddr,returnName,returnPhoneNum')
                    ->find();
                // 添加添加判断
                if (!$shopConfig['returnAddr']) {
                    $error = '请在店铺配置，先填写退货信息！';
                }
                $refundLog = '卖家同意退货申请，退货地址：' . $shopConfig['returnName'] . '，' . $shopConfig['returnPhoneNum'] . '，' . $shopConfig['returnAddr'];
                // 推送标题
                $pushLog = '卖家同意退货';

            } else {
                $error = '非法退款类型，请拒绝退款！';
            }
            if (!$error) {
                // 只有本地余额退款状态4,和先同意退款,需要商家继续退款状态1执行,状态7原路返回在回调更新状态
                if ($data['state'] == 1 || $data['state'] == 4) {
                    // 1.0.5 更新商品状态
                    $this->setOrderGoodsState($refund_status ?? 0, $row['goods_ids'], $row['order_type'], $row['order_id']);
                    // 更新订单状态
                    if ($row['type'] == 0) {
                        $this->setRefundState($row['order_id'], $row['order_type']);
                    }

                    //退回换购商品、优惠券
                    (new \app\api\model\wanlshop\Order())->returnBuyback($row['order_id'], $row['user_id']);
                }

                // 写入日志
                $this->refundLog($row['user_id'], $ids, $refundLog ?? '', $refundType ?? '', $row['shop_id']);
                // 推送开始
                $this->pushRefund($row['id'], $row['order_id'], $row['goods_ids'], $pushLog ?? '', $row['order_type']);
                // 更新退款
                $row->allowField(true)->save($data);
            }
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage() . ',line:' . $e->getLine());
        }
        if (!$error) {
            $this->success();
        } else {
            $this->error($error);
        }
    }

    /**
     * 确认收货
     */
    public function shopreceiving($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        //退款状态:0=申请退款,1=卖家同意,2=卖家拒绝,3=申请平台介入,4=成功退款,5=退款已关闭,6=已提交物流,7=第三方退款中,8=退款失败
        if ($row['state'] == 2 || $row['state'] == 3 || $row['state'] == 4 || $row['state'] == 5) {
            $this->error(__('当前状态，不可操作' . $row['state']));
        }
        // 退款订单状态
        $state = 4;
        // 退款日志
        $refundLog = '卖家确认收到退货，并将' . $row['price'] . '元退款到买家账号余额';

        // $refundType 退款状态:0=买家,1=卖家,2=官方,3=系统
        $refundType = 1;
        // 推送日志
        $pushLog = '退款已完成';
        $result = false;
        $library = new WanlPay();
        Db::startTrans();
        try {
            // 判断业务类型
            if ($row['order_type'] === 'groups') {
                $orderModel = model('app\index\model\wanlshop\groups\Order');
            } else {
                $orderModel = model('app\index\model\wanlshop\Order');
            }

            // 判断退款类型
            if ($row['type'] == 1) {
                // 判断金额
                if ($row['price'] > $row->pay->price) {
                    throw new Exception("非法退款金额，金额超过订单金额！！请拒绝退款！！");
                }
            } else {
                throw new Exception("非法退款类型，请拒绝退款！");
            }
            // 查询订单是已确定收货
            $order = $orderModel->get($row['order_id']);
            // 更新钱包
            // 1.此订单如果已确认收货扣商家
            // 2.此订单没有确认收货，平台退款
            if ($order['state'] == 4) {
                // 扣商家
                $library::money(-$row['price'], $order['shop']['user_id'], '确认收货，同意退款', 'refund', $order['order_no']);
            }

            // 退款给用户 (如果第三方退款提交成功后，扣除此款项)
            $library::money(+$row['price'], $row['user_id'], '卖家同意退款', 'refund', $order['order_no']);

            //退积分
            $this->returnScore($row['order_id'], $row['user_id'], $ids);

            // 1.1.5升级 第三方退款
            $config = get_addon_config('wanlshop');
            if ($config['config']['refund_switch'] == 'Y') {
                $refundType = 3;
                $pay = model('app\api\model\wanlshop\Pay')->get($row['order_pay_id']);
                if ($pay['pay_type'] != 0) {
                    $wanlpay = $library->refund($row['id'], $row['price'], $row['order_pay_id']);
                    // code=0同步方法：refund判断fund_change = Y，代表退款成功，支付宝文档《如何判断退款是否成功》，https://opendocs.alipay.com/support/01rawa
                    if ($wanlpay['code'] == 0) {
                        // 退款中
                        $data['state'] = 4;
                        // 退款日志
                        $refundLog = $wanlpay['data']['type_text'] . '已将退款￥' . $wanlpay['data']['money'] . '元原路返还买家' . $wanlpay['data']['type_text'] . '账户';
                        // 推送日志
                        $pushLog = $wanlpay['data']['type_text'] . '退款成功';
                        // 扣除本地用户余额
                        $library::money(-$wanlpay['data']['money'], $wanlpay['data']['user_id'], '退款订单（订单号：' . $order['order_no'] . '）已原路返回余额至你的' . $wanlpay['data']['type_text'], 'sys');
                        // 如果退款成功,修改退款状态为第三方退款中，等待回调,否则退款到余额
                    } else if ($wanlpay['code'] == 200) {
                        // 退款中
                        $data['state'] = 7;
                        // 退款日志
                        $refundLog = '已提交' . $wanlpay['data']['type_text'] . '处理，预计24小时内将退款￥' . $wanlpay['data']['money'] . '元原路返还买家' . $wanlpay['data']['type_text'] . '账户';
                        // 推送日志
                        $pushLog = $wanlpay['data']['type_text'] . '正在处理您的退款预计24小时内到账';
                        // 扣除本地用户余额
                        $library::money(-$wanlpay['data']['money'], $wanlpay['data']['user_id'], '退款订单（订单号：' . $order['order_no'] . '）已原路返回余额至你的' . $wanlpay['data']['type_text'], 'sys');
                    } else {
                        $refundLog = '卖家同意退款，第三方支付退款失败' . $row['price'] . '元退款到买家账号余额，请手动提现';
                    }
                }
            }
            // 只有本地余额退款状态4,状态7原路返回在回调更新状态
            if ($state == 4) {
                // 更新商品状态
                $this->setOrderGoodsState(3, $row['goods_ids'], $row['order_type'], $row['order_id']);
                // 更新订单状态
                $this->setRefundState($row['order_id'], $row['order_type']);
            }

            //退回换购商品、优惠券
            (new \app\api\model\wanlshop\Order())->returnBuyback($row['order_id'], $row['user_id']);

            // 写入日志
            $this->refundLog($row['user_id'], $ids, $refundLog, $refundType, $row['shop_id']);
            // 推送开始
            $this->pushRefund($row['id'], $row['order_id'], $row['goods_ids'], $pushLog, $row['order_type']);
            // 更新退款
            $result = $row->allowField(true)->save([
                'state' => $state,
                'completetime' => time()
            ]);
            //后续版本推送订购单
            // ...
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result !== false) {
            $this->success();
        } else {
            $this->error(__('No rows were updated'));
        }
    }

    /**
     * 拒绝退款
     */
    public function shoprefuse($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        //退款状态:0=申请退款,1=卖家同意,2=卖家拒绝,3=申请平台介入,4=成功退款,5=退款已关闭,6=已提交物流,7=第三方退款中,8=退款失败
        if ($row['state'] != 0) {
            $this->error(__('当前状态，不可操作' . $row['state']));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {

                $result = false;
                Db::startTrans();
                try {
                    $params['state'] = 2;
                    // 写入日志
                    $this->refundLog($row['user_id'], $row['id'], '卖家拒绝了您的退款申请，拒绝理由：' . $params['refuse_content'], $row['shop_id']);
                    // 更新商品状态
                    $this->setOrderGoodsState(5, $row['goods_ids'], $row['order_type'], $row['order_id']);
                    // 更新订单状态
                    $this->setRefundState($row['order_id'], $row['order_type']);
                    // 推送开始
                    $this->pushRefund($row['id'], $row['order_id'], $row['goods_ids'], '退款申请被拒绝', $row['order_type']);
                    // 更新退款
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException|PDOException|Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 推送退款消息（方法内使用）
     *
     * @param string refund_id 订单ID
     * @param string order_id 订单ID
     * @param string goods_id 订单ID
     * @param string title 标题
     */
    private function pushRefund($refund_id = 0, $order_id = 0, $goods_id = 0, $title = '', $order_type = 'goods')
    {
        if ($order_type === 'groups') {
            $orderModel = model('app\index\model\wanlshop\groups\Order');
            $orderGoodsModel = model('app\index\model\wanlshop\groups\OrderGoods');
        } else {
            $orderModel = model('app\index\model\wanlshop\Order');
            $orderGoodsModel = model('app\index\model\wanlshop\OrderGoods');
        }
        $order = $orderModel->get($order_id);
        $goods = $orderGoodsModel->get($goods_id);
        $shop = \app\admin\model\wanlshop\Shop::find($order->shop_id);
        $msg = [
            'user_id' => $order['user_id'], // 推送目标用户
            'shop_id' => $shop->id,
            'title' => $title,  // 推送标题
            'image' => $goods['image'], // 推送图片
            'content' => '您申请退款的商品 ' . (mb_strlen($goods['title'], 'utf8') >= 25 ? mb_substr($goods['title'], 0, 25, 'utf-8') . '...' : $goods['title']) . ' ' . $title,
            'type' => 'order',  // 推送类型
            'modules' => $order_type === 'groups' ? 'groupsrefund' : 'refund',  // 模块类型
            'modules_id' => $refund_id,  // 模块ID
            'come' => '订单' . $order['order_no'] // 来自
        ];
        $this->wanlchat->send($order['user_id'], $msg);
        $notice = model('app\index\model\wanlshop\Notice');
        $notice->data($msg);
        $notice->allowField(true)->save();
    }

    public function returnScore($oid, $uid, $ids)
    {
        $order = \app\api\model\wanlshop\Order::where(['id' => $oid, 'user_id' => $uid])->find();
        //扣除下单积分，增加抵扣积分
        $userFinanceModel = UserFinanceLog::where([
            'user_id' => $uid,
            'type' => 'score',
            'flag' => 'add',
            'rel_id' => $oid
        ])->find();
        if ($userFinanceModel) {
            $user = User::find($uid);
            UserFinanceLog::create([
                'user_id' => $uid,
                'type' => 'score',
                'flag' => 'reduce',
                'value' => $userFinanceModel->value,
                'before' => $user->score,
                'after' => bcsub($user->score, $userFinanceModel->value),
                'event' => 'refund',
                'rel_id' => $oid,
                'remark' => "订单退款,扣除增加的商品积分{$userFinanceModel->value},订单ID:{$oid},退款单号:{$ids}",
                'status' => 'succeed',
                'createtime' => time(),
                'updatetime' => time()
            ]);
            $user->score = bcsub($user->score, $userFinanceModel->value);
            $user->save();
        }
        //使用的积分
        if ($order->score > 0) {
            $user = User::find($uid);
            UserFinanceLog::create([
                'user_id' => $uid,
                'type' => 'score',
                'flag' => 'add',
                'value' => $order->score,
                'before' => $user->score,
                'after' => bcadd($user->score, $order->score),
                'event' => 'refund',
                'rel_id' => $oid,
                'remark' => "订单退款,返还抵扣的积分{$order->score},订单ID:{$oid},退款单号:{$ids}",
                'status' => 'succeed',
                'createtime' => time(),
                'updatetime' => time()
            ]);
            $user->score = bcadd($user->score, $order->score);
            $user->save();
        }
    }
}